/*-----------------------------------------------------------------------------
 * Jayson Dmello
 * ECEN 5613 Lab 3, Supplemental
 * Fall 2012, Prof. Mclure
 * University of Colorado,Boulder
 * -----------------------------------------------------------------------------
 * This program implements all the required functionalities to allow programs 
 * to successfully receive and transmit data using the pre-implemented
 * input-output functionalities and given hardware. Complete 
 * information on all methods in the code manual as well as io.h file
 *-----------------------------------------------------------------------------*


/*Includes*/
/*--------------------------------------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
#include<at89c51ed2.h>
#include <mcs51reg.h>

/*****************************************************************************/
/*Implements serial transmit using the polling method*/
void putchar (char c)
{
	while ((SCON & 0x02) == 0);    // wait for TX ready, spin on TI
	SBUF = c;  	// load serial port with transmit value
	TI = 0;  	// clear TI flag
}


/*****************************************************************************/
/*The function uses the putchar() method in an iterative manner to
   * transmit a complete string of characters.At the end of the character string 
   * transmission, a Null character (ASCII 13) is appended to the terminal 
   * output*/
void putstr (char *s)  //had return type int in sample file
{
	int i = 0;
	while (*s){			// output characters until NULL found
		putchar(*s++);
		i++;
	}
	putchar('\n');
	}
	
/*****************************************************************************/
/*Serial Reception using polling method*/	
char getchar ()__critical
{
	
    while (!RI);             
   	RI = 0;			// clear RI flag
	return SBUF;  	// return character from SBUF
}

/*****************************************************************************/
void printCustom(char * val){ //basic version of printf,loops putchar till NULL is encountered
	int i=0;
	while(val[i]!='\n'){
		putchar(val[i]);
		i++;
	}
}	


/*****************************************************************************/
/*The function implements a user input sequence. The user input is 
   * restricted to value of input parameter 'size'. Received characters are
   * stored at address locations pointed by char pointer val. The input 
   * sequence terminates under two conditions; either the size parameter
   * has reached or a Null character is received.*/		
void scanF(char* val,int size){  
		int i=0;
		char * addrSaver;
		char tmp;
		addrSaver=val;
		while ((tmp=getchar())!=13&&i<size)/*two conditions checked, as mentioned above*/
		{
		
			*addrSaver=tmp;	/*value is copied from tmp to address pointed by addrSaver*/
			addrSaver++;	/*increment pointer address location*/
			i++;
			putchar(tmp);	/*Echo characters back*/
			
		}
		val[i+1]='\n';		/*Append a Null character to the received characters*/
		
		
	}
	
/*****************************************************************************/
/*The function prints the terminal clear screen character sequence
* "\033[2J". Can be effectively used to clear up cluttered terminal screen*/
	void clrScr()
	{
		printf_tiny("\033[2J");
	}
	
/*****************************************************************************/	
/*The function prints a line in dashed characters*/	
	void newLine()
		{
			printf_tiny("--------------------------------------------------------------------------------\r\n");
		}
		
/*****************************************************************************/	


/*The function reads value from the TH0 and TL0 registers
* of counter 0. The count is calculated for a 16-bit timer, that is
* TH0 is multiplied by 256 and added to TL0.*/		
	void readCount()
	{
		int Count=0;
			printf("%d:%d",TH0,TL0);
			Count=TH0*255+TL0;
			if(TH0>0&&TL0==0)
			{
			Count++;
			}
		
		
		
		printf("Counter 0:%d\r\n",Count);
	}
	